package cn.wit.test;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.Axis;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class DemoLineChart extends Application{

	@Override
	public void start(Stage primaryStage) throws Exception {
		// TODO Auto-generated method stub
		
		VBox box = new VBox();
		
		Map<String,Number> scores = new HashMap<>();
		scores.put("张三", 99);
		scores.put("李四", 29);
		scores.put("王五", 9);
		
		box.getChildren().add(createLineChart("语文分数",scores));
		
		Scene scene = new Scene(box);
		
		primaryStage.setScene(scene);
		
		primaryStage.show();
	}
	
	public static void main(String[] args) {
		launch(args);
	}
	
	public LineChart<String, Number> createLineChart(String title,Map<String,Number> scores){
	
		//创建x轴和y轴
		Axis<String> xAxis = new CategoryAxis();
		
		Axis<Number> yAxis = new NumberAxis();
		
		yAxis.setLabel("分数");
		xAxis.setLabel("姓名");
		
		LineChart<String,Number> lineChart = new LineChart<>(xAxis , yAxis);
		
		Series<String,Number>  series = new Series<>();
		
		for(Entry<String, Number> entry:scores.entrySet()) {
			series.getData().add(new Data<String,Number>(entry.getKey(),entry.getValue()));
		}
		
		lineChart.getData().add(series);
		return lineChart;
	}

}
